﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace LockScreen
{
    internal class Tools
    {
        // 打开输入桌面
        [DllImport("user32.dll", SetLastError = true)]
        private static extern IntPtr OpenInputDesktop(uint dwFlags, bool fInherit, uint dwDesiredAccess);


        // 锁定工作站
        [DllImport("user32.dll")]
        private static extern bool LockWorkStation(); // 锁屏

        // 关闭显示器
        const int WM_SYSCOMMAND = 0x0112;
        const int SC_MONITORPOWER = 0xF170;
        const int HWND_BROADCAST = 0xFFFF;

        [DllImport("user32.dll", SetLastError = true)]
        static extern bool PostMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);

        /// <summary>
        /// 锁屏 
        /// </summary>
        /// <returns></returns>
        public static void Locked()
        {
            LockWorkStation(); // 锁屏 
        }

        /// <summary>
        /// 关闭显示器
        /// lParam: 2 = 关闭显示器, 1 = 省电, -1 = 开启
        /// </summary>
        public static void TurnOffMonitor()
        {
            //PostMessage((IntPtr)HWND_BROADCAST, WM_SYSCOMMAND, (IntPtr)SC_MONITORPOWER, (IntPtr)2);//广播关闭显示器 异步 
            RunNirCmdMonitorOff("monitor off");
        }

        /// <summary>
        /// 打开显示器
        /// lParam: 2 = 关闭显示器, 1 = 省电, -1 = 开启
        /// </summary>
        public static void TurnOnMonitor()
        {
            //RunCmd("nircmd.exe sendmouse move 1 1");
            //RunCmd("nircmd.exe sendkeypress ctrl");
            //RunNirCmdMonitorOff("sendkeypress ctrl");


            //[DllImport("user32.dll")]
            //static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint dwData, UIntPtr dwExtraInfo);

            //const uint MOUSEEVENTF_MOVE = 0x0001;

            ////PostMessage((IntPtr)HWND_BROADCAST, WM_SYSCOMMAND, (IntPtr)SC_MONITORPOWER, (IntPtr)(-1));//广播打开显示器 异步

            //// 移动鼠标 1 像素然后再移动回来
            //mouse_event(MOUSEEVENTF_MOVE, 1, 0, 0, UIntPtr.Zero);
            //mouse_event(MOUSEEVENTF_MOVE, unchecked((uint)-1), 0, 0, UIntPtr.Zero);
        }


        /// <summary>
        /// 启动 nircmd.exe 执行关闭显示器命令
        /// 如果 nircmd.exe 已经在运行，则不会重复启动
        /// </summary>
        private static void RunNirCmdMonitorOff(string arguments)
        {
            // 检查系统中是否已经存在 nircmd.exe 进程
            if (!Process.GetProcessesByName("nircmd").Any())
            {
                ProcessStartInfo psi = new ProcessStartInfo
                {
                    FileName = "nircmd.exe",  // nircmd.exe 路径，如果在系统 PATH 中就直接写文件名
                    Arguments = arguments, // nircmd 参数
                    UseShellExecute = false,
                    CreateNoWindow = true
                };

                // 启动 nircmd.exe，但不等待它退出
                Process.Start(psi);

                Console.WriteLine("nircmd 已启动，执行关闭显示器命令。");
            }
            else
            {
                Console.WriteLine("nircmd 已经在运行，不重复启动。");
            }
        }

        /// <summary>
        /// 检测设置窗口是否打开
        /// </summary>
        /// <returns></returns>
        public static bool IsSettingsWindowOpen()
        {
            // 获取所有正在运行的进程
            Process[] processes = Process.GetProcessesByName("SystemSettings");

            // 如果存在至少一个运行的 SystemSettings.exe 进程，返回 true
            foreach (Process proc in processes)
            {
                if (!proc.HasExited && proc.Responding)
                {
                    return true;
                }
            }
            return false;
        }
    }
}
